package cc.yaomm.common.basic.config;

import cc.yaomm.common.basic.captcha.CaptchaBusiness;
import cc.yaomm.common.basic.captcha.config.CaptchaProperties;
import cc.yaomm.common.basic.captcha.generator.CaptchaGenerator;
import cc.yaomm.common.basic.captcha.handler.CaptchaHandler;
import cc.yaomm.common.basic.captcha.handler.EmailCaptchaHandler;
import cc.yaomm.common.basic.captcha.handler.ImageCaptchaHandler;
import cc.yaomm.common.basic.captcha.handler.SmsCaptchaHandler;
import cc.yaomm.common.basic.enums.CaptchaType;
import cc.yaomm.common.basic.file.AliOSSService;
import cc.yaomm.common.basic.file.FileService;
import cc.yaomm.common.basic.file.LocalDiskService;
import cc.yaomm.common.basic.file.properties.AliOSSProperties;
import cc.yaomm.common.basic.file.properties.FileProperties;
import cc.yaomm.common.basic.sms.AliSmsService;
import cc.yaomm.common.basic.sms.HuaweiSmsService;
import cc.yaomm.common.basic.sms.SmsService;
import cc.yaomm.common.basic.sms.properties.AliSmsProperties;
import cc.yaomm.common.basic.sms.properties.HuaweiSmsProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by XQL on 2020/5/15.
 */
@Configuration
@EnableConfigurationProperties({AliSmsProperties.class, HuaweiSmsProperties.class,
        CaptchaProperties.class, FileProperties.class, AliOSSProperties.class})
public class AutoConfiguration {

    @Bean
    @ConditionalOnProperty(prefix = "captcha", value = "enable", havingValue = "true")
    public CaptchaGenerator captchaGenerator() {
        return new CaptchaGenerator();
    }

    @Bean
    @ConditionalOnProperty(prefix = "captcha", value = "enable", havingValue = "true")
    public CaptchaBusiness captchaBusiness() {
        return new CaptchaBusiness();
    }

    @Bean
    @ConditionalOnProperty(prefix = "captcha", value = "enable", havingValue = "true")
    public ImageCaptchaHandler imageCaptchaHandler() {
        return new ImageCaptchaHandler();
    }

    @Bean("aliSmsService")
    @Primary
    @ConditionalOnProperty(prefix = AliSmsProperties.ALI_SMS_PREFIX, value = "app-key")
    public SmsService aliSmsService() {
        return new AliSmsService();
    }

    @Bean("huaweiSmsService")
    @ConditionalOnProperty(prefix = HuaweiSmsProperties.HUAWEI_SMS_PREFIX, value = "app-key")
    public SmsService huaweiSmsService() {
        return new HuaweiSmsService();
    }

    @Bean
    @ConditionalOnBean(SmsService.class)
    public SmsCaptchaHandler smsCaptchaHandler() {
        return new SmsCaptchaHandler();
    }

    @Bean
    @ConditionalOnProperty(prefix = "captcha.mail", value = "enable", havingValue = "true")
    public EmailCaptchaHandler emailCaptchaHandler() {
        return new EmailCaptchaHandler();
    }

    @Bean
    @ConditionalOnProperty(prefix = CaptchaProperties.CAPTCHA_PREFIX, value = "enable", havingValue = "true")
    public Map<CaptchaType, CaptchaHandler> captchaHandlerMap(
            @Autowired(required = false) SmsService smsService,
            @Autowired(required = false) EmailCaptchaHandler emailCaptchaHandler) {
        Map<CaptchaType, CaptchaHandler> captchaHandlerMap = new HashMap<>();
        captchaHandlerMap.put(CaptchaType.IMAGE, this.imageCaptchaHandler());

        //判断是否加载了短信服务
        if (smsService != null) {
            captchaHandlerMap.put(CaptchaType.SMS, this.smsCaptchaHandler());
        }

        //判断是否加载了邮件服务
        if (emailCaptchaHandler != null) {
            captchaHandlerMap.put(CaptchaType.EMAIL, this.emailCaptchaHandler());
        }

        return captchaHandlerMap;
    }

    @Bean
    @ConditionalOnProperty(prefix = AliOSSProperties.ALI_OSS_PREFIX, value = "app-key")
    public FileService aliOSSService() {
        return new AliOSSService();
    }

    /**
     * 此bean加载必须放在最后，优先加载其他方式
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(FileService.class)
    public FileService localDiskService() {
        return new LocalDiskService();
    }

}
